import dictionary from '@/utils/dictionary'
export default function (code) {
  const space = [' ', '\r', '\t', '\n']
  // 最后结果
  const result = []
  // 报错
  const errorarr = []
  code = code.slice(6, code.length - 5)
  let i = 0
  let limit = 0
  // eslint-disable-next-line
  let line = 1 // 当前行数
  const numback = [
    ' ',
    '\n',
    '\r',
    '\t',
    ']',
    ')',
    '/',
    '+',
    '-',
    '*',
    '%',
    ';',
    ','
  ]
  while (i < code.length && limit < 1000) {
    limit++
    let str = ''
    let state = 0
    if (code[i] === '\n') {
      line++
      i++
    } else if (['\r', ' ', '\t'].includes(code[i])) {
      i++
    } else if (/^[a-zA-Z]$/.test(code[i])) {
      // 变量名
      str += code[i]
      i++
      while (/^[a-zA-Z0-9]$/.test(code[i])) {
        str += code[i]
        i++
      }
      if (dictionary[str]) {
        result.push({ key: str, value: dictionary[str] })
      } else {
        result.push({ key: str, value: dictionary['标识符'] })
      }
    } else if (
      [
        '(',
        ')',
        '{',
        '}',
        '.',
        "'",
        '"',
        ':',
        '~',
        ',',
        '[',
        ']',
        ';'
      ].includes(code[i])
    ) {
      str = code[i]
      i++
      if (dictionary[str]) {
        result.push({ key: str, value: dictionary[str] })
      } else {
        console.log('不存在')
      }
    } else if (/^[0-9]$/.test(code[i])) {
      // 记录起始位置，预防报错
      const start = i
      // 十进制,八进制和十六进制
      while (![20, 77].includes(state)) {
        switch (state) {
          case 0:
            if (/^[1-9]$/.test(code[i])) {
              state = 1
              str += code[i]
              i++
            } else if (code[i] === '0') {
              state = 3
              str += code[i]
              i++
            }
            break
          case 1:
            // 十进制
            if (/^[0-9]$/.test(code[i])) {
              state = 1
              str += code[i]
              i++
            } else if (code[i] === '.') {
              state = 8
              str += code[i]
              i++
            } else if (['e', 'E'].includes(code[i])) {
              state = 10
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
          case 3:
            // 16进制，8进制或者以0开头的小数
            if (/^[0-7]$/.test(code[i])) {
              state = 3
              str += code[i]
              i++
            } else if (code[i].toLowerCase() === 'x') {
              state = 5
              str += code[i]
              i++
            } else if (code[i] === '.') {
              state = 8
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
          case 5:
            // 16进制
            if (/^[0-9a-fA-F]$/.test(code[i])) {
              state = 6
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
          case 6:
            // 16进制
            if (/^[0-9a-fA-F]$/.test(code[i])) {
              state = 6
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
          case 8:
            // 小数1.
            if (/^[0-9]$/.test(code[i])) {
              state = 9
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
          case 9:
            // 小数1.9
            if (/^[0-9]$/.test(code[i])) {
              state = 9
              str += code[i]
              i++
            } else if (['e', 'E'].includes(code[i])) {
              state = 10
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
          case 10:
            // 科学进制1.9e
            if (['+', '-'].includes(code[i])) {
              state = 11
              str += code[i]
              i++
            } else if (/^[0-9]$/.test(code[i])) {
              state = 12
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
          case 11:
            // 科学进制，带正负号('-'和'+') 1E-
            if (/^[0-9]$/.test(code[i])) {
              state = 12
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
          case 12:
            // 科学进制，不带正负号('-'和'+') 1E10
            if (/^[0-9]$/.test(code[i])) {
              state = 12
              str += code[i]
              i++
            } else if (numback.includes(code[i])) {
              if (code[i] === '\n') {
                line++
              }
              state = 20
            } else {
              while (!space.includes(code[i])) {
                i++
              }
              i++
              state = 77
            }
            break
        }
      }
      if (state === 20) {
        result.push({ key: str, value: dictionary['实数'] })
      } else if (state === 77) {
        errorarr.push({ line, content: code.slice(start, i) })
      }
    } else if (code[i] === '>') {
      state = 1
      str += code[i++]
      while (![2, 4, 5, 6].includes(state)) {
        switch (state) {
          case 1:
            if (code[i] === '=') {
              state = 2
              str += code[i]
              i++
            } else if (code[i] === '>') {
              state = 3
              str += code[i]
              i++
            } else {
              if (code[i] === '\n') {
                line++
                i++
              }
              state = 6
            }
            break
          case 3:
            if (code[i] === '=') {
              state = 4
              str += code[i]
              i++
            } else {
              if (code[i] === '\n') {
                line++
                i++
              }
              state = 5
            }
            break
        }
      }
      result.push({ key: str, value: dictionary[str] })
    } else if (code[i] === '<') {
      state = 1
      str += code[i++]
      while (![2, 4, 5, 6].includes(state)) {
        switch (state) {
          case 1:
            if (code[i] === '=') {
              state = 2
              str += code[i]
              i++
            } else if (code[i] === '<') {
              state = 3
              str += code[i]
              i++
            } else {
              if (code[i] === '\n') {
                line++
                i++
              }
              state = 6
            }
            break
          case 3:
            if (code[i] === '=') {
              state = 4
              str += code[i]
              i++
            } else {
              if (code[i] === '\n') {
                line++
                i++
              }
              state = 5
            }
            break
        }
      }
      result.push({ key: str, value: dictionary[str] })
    } else if (code[i] === '+') {
      while (state !== 20) {
        switch (state) {
          case 0:
            if (code[i] === '+') {
              state = 1
              str += code[i]
              i++
            } else {
              state = 20
            }
            break
          case 1:
            if (['+', '='].includes(code[i])) {
              str += code[i]
              i++
              state = 20
            } else {
              if (code[i] === '\n') {
                line++
                i++
              }
              state = 20
            }
            break
        }
      }
      result.push({ key: str, value: dictionary[str] })
    } else if (code[i] === '-') {
      while (state !== 20) {
        switch (state) {
          case 0:
            if (code[i] === '-') {
              state = 1
              str += code[i]
              i++
            } else {
              state = 20
            }
            break
          case 1:
            if (['-', '=', '>'].includes(code[i])) {
              str += code[i]
              i++
              state = 20
            } else {
              if (code[i] === '\n') {
                line++
                i++
              }
              state = 20
            }
            break
        }
      }
      result.push({ key: str, value: dictionary[str] })
    } else if (['*', '^', '!', '='].includes(code[i])) {
      str += code[i]
      if (code[i + 1] === '=') {
        i++
        str += code[i]
        i++
      } else {
        if (code[i + 1] === '\n') {
          line++
          i++
        }
        i++
      }
      result.push({ key: str, value: dictionary[str] })
    } else if (code[i] === '&') {
      str += code[i]
      if (['=', '&'].includes(code[i + 1])) {
        i++
        str += code[i]
        i++
      } else {
        if (code[i + 1] === '\n') {
          line++
          i++
        }
        i++
      }
      result.push({ key: str, value: dictionary[str] })
    } else if (code[i] === '/') {
      while (![20, 21].includes(state)) {
        switch (state) {
          case 0:
            if (code[i] === '/') {
              state = 1
              str += code[i]
              i++
            } else {
              state = 20
            }
            break
          case 1:
            if (code[i] === '/') {
              state = 2
              i++
            } else if (code[i] === '*') {
              state = 3
              i++
            } else if (code[i] === '=') {
              state = 21
              str += code[i]
              i++
            } else {
              state = 21
            }
            break
          case 2:
            if (code[i] !== '\n') {
              state = 2
              i++
            } else {
              line++
              state = 20
              i++
            }
            break
          case 3:
            if (code[i] !== '*' || (code[i] === '*' && code[i + 1] !== '/')) {
              if (code[i] === '\n') line++
              state = 3
              i++
            } else {
              state = 20
              i += 2
            }
            break
        }
      }
      if (state === 21) {
        result.push({ key: str, value: dictionary[str] })
      }
    } else if (code[i] === '%') {
      while (state !== 20) {
        switch (state) {
          case 0:
            if (code[i] === '%') {
              state = 1
              str += code[i]
              i++
            }
            break
          case 1:
            if (code[i] === '=') {
              state = 20
              str += code[i]
              i++
            } else if (['d', 'c', 's', 'f'].includes(code[i])) {
              state = 20
              str += code[i]
              i++
            } else {
              if (code[i] === '\n') {
                line++
                i++
              }
              state = 20
            }
        }
      }
      result.push({ key: str, value: dictionary[str] })
    } else if (code[i] === '\\') {
      str += '\\'
      if (code[i + 1] === 'n') {
        i++
        str += code[i]
        i++
      }
      result.push({ key: str, value: dictionary[str] })
    } else if (code[i] === '|') {
      str += code[i]
      i++
      if (['|', '='].includes(code[i])) {
        str += code[i]
        i++
      }
      result.push({ key: str, value: dictionary[str] })
    }
  }

  return { result, errorarr }
}
